
package org.jetnuke.dao.generic;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import net.jforum.JForumExecutionContext;
import net.jforum.dao.generic.AutoKeys;
import net.jforum.util.preferences.SystemGlobals;

import org.jetnuke.entities.StoriesCat;

public class GenericStoriesCatDAO extends AutoKeys implements org.jetnuke.dao.StoriesCatDAO {

	public StoriesCat retrieveByPK(int storyId) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("StoryCatModel.selectByID"));
		p.setInt(1, storyId);
		
		ResultSet rs = p.executeQuery();
		StoriesCat s = new StoriesCat();
		
		if (rs.next()) {
			fillStoryCatFromResultSet(s, rs);
		}
		
		rs.close();
		p.close();

		return s;
	}

	/**
	 * @param s
	 * @param rs
	 */
	protected void fillStoryCatFromResultSet(StoriesCat s, ResultSet rs) throws Exception{
		s.setCATID(rs.getInt("CATID"));
		s.setCOUNTER(rs.getInt("COUNTER"));
		s.setTITLE(rs.getString("TITLE"));
		
	}
	protected List processSelectAll(ResultSet rs)throws Exception {
		List list = new ArrayList();
		while (rs.next()) {
			StoriesCat u = new StoriesCat();
			fillStoryCatFromResultSet(u,rs);			
			list.add(u);
		}
		
		return list;
	}

	/* (non-Javadoc)
	 * @see org.jetnuke.model.StoriesCatModel#selectAll()
	 */
	public List selectAll() throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("StoryCatModel.selectAll"));
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();
		
		return list;
	}

	/* (non-Javadoc)
	 * @see org.jetnuke.model.StoriesCatModel#selectAllOrderTitle()
	 */
	public List selectAllOrderTitle() throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("StoryCatModel.selectAllOrderTitle"));
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();
		
		return list;

	}

	/* (non-Javadoc)
	 * @see org.jetnuke.model.StoriesCatModel#selectOrderCounterDesc(int)
	 */
	public List selectOrderCounterDesc(int limit) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("StoryCatModel.selectOrderCounterDesc"));
		p.setInt(1,limit);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();
		
		return list;
	}

	/* (non-Javadoc)
	 * @see org.jetnuke.model.StoriesCatModel#addNew(org.jetnuke.entities.StoriesCat)
	 */
	public int addNew(StoriesCat cat) throws Exception {
		PreparedStatement p = this.getStatementForAutoKeys("StoryCatModel.addNew");
		this.setAutoGeneratedKeysQuery(SystemGlobals.getSql("StoriesCatModel.lastGeneratedStoryCatID"));
		initNewStoryCat(cat, p);
		
		int id = this.executeAutoKeysQuery(p);
		p.close();		
		return id;	
		
	}

	/**
	 * @param cat
	 * @param p
	 */
	private void initNewStoryCat(StoriesCat cat, PreparedStatement p)throws Exception {
		p.setString(1,cat.getTITLE());
		p.setInt(2,cat.getCOUNTER());
		
	}

	/* (non-Javadoc)
	 * @see org.jetnuke.model.StoriesCatModel#update(org.jetnuke.entities.StoriesCat)
	 */
	public void update(StoriesCat cat) throws Exception {
		PreparedStatement p= JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("StoryCatModel.update"));
		p.setInt(1,cat.getCOUNTER());
		p.setString(2,cat.getTITLE());
		p.setInt(3,cat.getCATID());
		p.executeUpdate();
		p.close();
		
	}

	/* (non-Javadoc)
	 * @see org.jetnuke.model.StoriesCatModel#deleteByPK(int)
	 */
	public void deleteByPK(int storyCatId) throws Exception {
		PreparedStatement p= JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("StoryCatModel.deleteByID"));
		p.setInt(1,storyCatId);
		p.executeUpdate();
		p.close();
		
	}

	/* (non-Javadoc)
	 * @see org.jetnuke.model.StoriesCatModel#incCounter(int)
	 */
	public void incCounter(int catid) throws Exception {
		PreparedStatement p= JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("StoryCatModel.incCounter"));
		p.setInt(1,catid);
		p.executeUpdate();
		p.close();
		
	}

}
